package org.bcncoding.appengine.droiding.server.dao;

import java.util.List;

import javax.jdo.Query;

import org.bcncoding.appengine.droiding.server.entity.ServiceProvider;

public class ServiceProviderDao extends AbstractDao<ServiceProvider> {

	public ServiceProviderDao(Class<ServiceProvider> type) {
		super(type);
	}

	@SuppressWarnings("unchecked")
	public List<ServiceProvider> getAllPaged(int startRow, int maxRow) {
		String query = "SELECT FROM " + type.getName();
		Query q = pm.newQuery(query);
		q.setRange(startRow, startRow + maxRow);
		return (List<ServiceProvider>) q.execute();
	}
	
	public Integer count() {
		//FIXME Si se hace count(*) da error a runtime... buscar la manera.
		String query = "SELECT count(Id) FROM " + type.getName();
		Query q = pm.newQuery(query);
		return (Integer) q.execute();
	}
	
	@SuppressWarnings("unchecked")
	public ServiceProvider getByKey(String key) {
		Query q = pm.newQuery(type);
	    q.setFilter("key == pKey");
	    q.declareParameters("String pKey");
	    //FIXME Cuidado con la key!!!!
	    List<ServiceProvider> result = (List<ServiceProvider>) q.execute("key " + key);
	    if (result.size() != 1)
	    	return null;
	    return result.get(0);
	}
	
}
